
// g++ xtest.cc -o xtest -lX11 -lXtst

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 */

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <iostream>

using namespace std;

int  main(int argc, char *argv[])
{
    Display  *dpy;
    int       i ;
    
    if(!(dpy = XOpenDisplay(NULL)))
    {
        cout << "Cannot open display" << endl;
        return 1;
    }
    
    int  event_base, error_base;
    int  major_version, minor_version;
    if(!XTestQueryExtension(dpy,
                            &event_base,
                            &error_base,
                            &major_version,
                            &minor_version))
    {
        cout << "XTest extension not supported" << endl;
        XCloseDisplay(dpy);
        return 1;
    }
    cout << "xorg server " << DisplayString(dpy) << endl;
    cout << "major version " << major_version << endl;
    cout << "minor version " << minor_version << endl;
    
    /* execute requests even if server is grabbed */
    /* ... not needed in this example !!!         */
    XTestGrabControl(dpy, True);
    
    /* ******************************************************* */
    /* Here follows some examples on how tu use XTestFake___() */
    /* ******************************************************* */
    XSync(dpy,True);
    XTestFakeMotionEvent(dpy,  /* display        */
                        0,    /* this screen    */
                        10,   /* x ccord = 10   */
                        10,   /* y coord. = 10  */
                        20    /* delay 20 milli-seconds */
                        );
    for (i=1 ; i<50 ; i++)
        XTestFakeMotionEvent(dpy,0 , 16*i, 16*i, 20);
    for (i=1 ; i<50 ; i++)
        XTestFakeMotionEvent(dpy,0 , 800-i, 800-i*16, 20);
    for (i=1 ; i<50 ; i++)
        XTestFakeMotionEvent(dpy,0 , 800-i*15, i*16, 20);
    
    XTestFakeKeyEvent(dpy,         /* display */
                      XKeysymToKeycode(dpy, XStringToKeysym ("p")),  /* what to write */
                      True,        /* Press */
                      CurrentTime  /* delay milli-seconds */
                      );
    XTestFakeKeyEvent(dpy,         /* display */
                      XKeysymToKeycode(dpy, XStringToKeysym ("p")),  /* what to write */
                      False,       /* Release */
                      CurrentTime  /* delay milli-seconds */
                      );
    XTestFakeKeyEvent(dpy, XKeysymToKeycode(dpy, XStringToKeysym ("w")), True , CurrentTime);
    XTestFakeKeyEvent(dpy, XKeysymToKeycode(dpy, XStringToKeysym ("w")), False, CurrentTime);
    XTestFakeKeyEvent(dpy, XKeysymToKeycode(dpy, XStringToKeysym ("d")), True , CurrentTime);
    XTestFakeKeyEvent(dpy, XKeysymToKeycode(dpy, XStringToKeysym ("d")), False, CurrentTime);
    XTestFakeKeyEvent(dpy, XKeysymToKeycode(dpy, XK_Return), True, CurrentTime);
    XTestFakeKeyEvent(dpy, XKeysymToKeycode(dpy, XK_Return), False, CurrentTime);
    
    XTestFakeButtonEvent(dpy, 1, True, 10);
    for (i=1 ; i<50 ; i++)
        XTestFakeMotionEvent(dpy,0 , 800-i*15, i*16, 20);
    XTestFakeMotionEvent (dpy, 0, 800, 0, 20);
    XTestFakeButtonEvent(dpy, 1, False, 100);
    
    XFlush(dpy);
}
